package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    /**
     * 根据课程ID查询课程相关信息
     * @param request
     * @param response
     */
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response) {

        try {
            String courseId = request.getParameter("id");

            CourseContentService courseContentService = new CourseContentServiceImpl();

            List<Course_Section> sectionList = courseContentService.findSectionAndLessonByCourseId(Integer.parseInt(courseId));

            String jsonString = JSON.toJSONString(sectionList);

            response.getWriter().print(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加章节时进行数据回显
     * @param request
     * @param response
     */
    public void findCourseByCourseId(HttpServletRequest request, HttpServletResponse response) {

        try {
            String courseId = request.getParameter("id");

            CourseContentService courseContentService = new CourseContentServiceImpl();

            Course course = courseContentService.findCourseByCourseId(Integer.parseInt(courseId));

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");
            String jsonString = JSON.toJSONString(course, filter);

            response.getWriter().print(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存章节信息
     * @param request
     * @param response
     */
    public void saveSection(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> map = (Map<String, Object>) request.getAttribute("map");

            Course_Section section = new Course_Section();

            BeanUtils.populate(section, map);

            CourseContentService courseContentService = new CourseContentServiceImpl();

            String result = courseContentService.saveSection(section);

            response.getWriter().print(result);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存或修改章节信息
     * @param request
     * @param response
     */
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response){
        try { //1.获取参数
            Map<String, Object> map =(Map) request.getAttribute("map");

            //2.创建 Course_Section
            Course_Section section = new Course_Section();

            //3.使用BeanUtils,将map中的数据封装到section对象里
            BeanUtils.copyProperties(section,map.get("section"));

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            if(section.getId() != 0){ //修改操作
                String result = contentService.updateSection(section);

                //5.返回结果数据
                response.getWriter().println(result);
            }else{ //添加操作
                String result = contentService.saveSection(section);

                //5.返回结果数据
                response.getWriter().println(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改章节状态
     * @param request
     * @param response
     */
    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response){
        try { //获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));

            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            String result = contentService.updateSectionStatus(id, status);

            //返回结果数据findSectionById
            response.getWriter().println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存&修改课时接口
     * @param request
     * @param response
     */
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.获取参数
            Map<String, Object> map =(Map) request.getAttribute("map");

            //2.创建 Course_Section
            Course_Lesson lesson = new Course_Lesson();

            //3.使用BeanUtils,将map中的数据封装到lesson对象里
            BeanUtils.copyProperties(lesson, map);

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            if(lesson.getId() != 0){ //修改操作
                String result = contentService.updateLesson(lesson);

                //5.返回结果数据
                response.getWriter().println(result);
            }else{ //添加操作
                String result = contentService.saveLesson(lesson);

                //5.返回结果数据
                response.getWriter().println(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
